 
 
let { loadImage } = require('canvas')
 let  { _drawRadiusRect} = require('./tool')
// import { toPx ,_drawRadiusRect,toRpx} from "./tool";
//绘制图片
 function drawImage({ options, data }) {
  return new Promise(async resolve => {
    try {
      
    
    const { ctx,canvas ,pixelRatio } = options;
    // const { url, x, y, w, h, sx, sy, sw, sh, borderRadius = 0, borderWidth = 0, borderColor } = data;
    const { x, y, url, zIndex ,borderRadius =0 ,borderWidth,borderColor,height:setHeight,width:setWidth} = data;
      // console.log('url',url);
      
      let imgInfo = await loadImage(url)
    let sx;
    let sy;
    const width = imgInfo.width / pixelRatio;
    const height = imgInfo.height / pixelRatio;

    if (width / height <= setWidth / setHeight) {
      sx = 0;
      sy = (height - ((width / setWidth) * setHeight)) / 2;
    } else {
      sy = 0;
      sx = (width - ((height / setHeight) * setWidth)) / 2;
    }
    let result = [
      imgInfo,
      sx,
      sy,
       (width - (sx * 2)),
      (height - (sy * 2)),
      x,
      y,
       setWidth,
      setHeight,]
    
    ctx.save();
    if (borderRadius > 0) {

      let data = {
        x, y, w:setWidth, h:setHeight,
        r: borderRadius
      };
      _drawRadiusRect({data, options});
      ctx.strokeStyle = 'rgba(255,255,255,0)';
      ctx.stroke();
      ctx.clip();

      ctx.drawImage(...result);
      if (borderWidth > 0) {
        if (borderColor) { ctx.strokeStyle = borderColor; }
        ctx.lineWidth = borderWidth;
        ctx.stroke();
      }
    } else {
      
      ctx.drawImage(...result);
    }
    ctx.restore();
      resolve()
    
    } catch (error) {
      resolve()
      
    }
    })
   
  
}
 
module.exports =drawImage